(function (vc) {

    vc.extends({
        data: {
            loginInfo: {
                logo: '',
                username: '',
                passwd: '',
                validateCode: '',
                errorInfo: '',
                codeImage: '',
            }
        },
        _initMethod: function () {
            $that.generateCode();
        },
        _initEvent: function () {

        },
        methods: {
            generateCode: function() {
                var param = {
                    params: {
                        _uId: '123'
                    }
                };
                vc.http.apiGet('/login.generatorValidateCode',
                    param,
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            vc.component.loginInfo.codeImage = json;
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                    });

            },
            doLogin: function() {
                if (!vc.notNull(vc.component.loginInfo.username)) {
                    vc.toast('用户名不能为空');
                    return;
                }
                if (!vc.notNull(vc.component.loginInfo.passwd)) {
                    vc.toast('密码不能为空');
                    return;
                }
                if (!vc.notNull(vc.component.loginInfo.validateCode)) {
                    vc.toast('验证码不能为空');
                    return;
                }
                vc.http.apiPost(
                    '/login.supplyChainUserLogin',
                    JSON.stringify(vc.component.loginInfo), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _data = JSON.parse(json);
                        if (_data.hasOwnProperty('code') && _data.code != '0') {
                            vc.toast(_data.msg);
                            return;
                        }
                        if (_data.code == 0) {
                            var param = {
                                url: '/'
                            };
                            if (_data.levelCd == '03') {
                                vcFramework.saveData('/nav/getUserInfo', _data);
                                window.location.href= '/page.html';
                                //window.location.href= return_url ? return_url :'/page.html';有返回url的地址
                            } else{
                                vc.toast("非采购用户请从供应商、代理商入口登录");
                            }
                            //  if (_data.levelCd == '04') {
                            //     $that.validateHasStore(param);
                            // } else if (_data.levelCd == '05') {
                            //     $that.validateHasStore(param);
                            // }
                            return;
                        }
                        vc.toast(json);
                        vc.component.loginInfo.errorInfo = json;
                    },
                    function(errInfo, error) {
                        vc.toast(errInfo);
                        vc.component.loginInfo.errorInfo = errInfo;
                    });

            },
             validateHasStore:function(_param) {
                var param = {
                    params: {
                        _uId: 'ccdd00opikookjuhyyttvhnnjuuu'
                    }
                };
                vc.http.apiGet('/store.listMyStore',
                    param,
                    function(json, res) {
                        let _data = JSON.parse(json);
                        if (res.status == 200) {
                            if (_data.code == 0) {
                                $that.loadStaffPrivileges(_param);
                                window.localStorage.setItem('hc_currentStoreInfo', JSON.stringify(res.data[0]));
                            } else {
                                vc.toast('登陆失败，请您稍后再试');
                            }
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                    });
            },
             loadStaffPrivileges:function(_param) {
                var param = {
                    params: {
                        a: 'HC'
                    }
                };
                vc.http.apiGet('/privilege.queryStaffPrivileges',
                    param,
                    function(json, res) {
                        let _data = JSON.parse(json);
                        if (res.status == 200) {
                            let _privilege = [];
                            _data.datas.forEach(item => {
                                _privilege.push(item.pId);
                            });
                        window.localStorage.setItem('hc_staff_privilege', JSON.stringify(_privilege));
                        window.location.href = _param.url;
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                    });
            },
            _doRegister: function() {
                vc.jumpToPage('/pageLR.html#/pages/plateform/register');
            },

        }
    });

})(window.vc);
